package ggmines

import (
	"sync"
	"wzgames/internal/model/statement"
	"wzgames/internal/secure"
	"wzgames/internal/services/games"
	"wzgames/pkg/utils/byteutil"
)

type GMarker struct {
	games.Marker
	mu sync.Mutex

	serverSeed string
	nextSeed   string

	betQ *statement.PlayBet
	betR *statement.MinesBetInfo

	inOpen bool
}

func NewGMarker() games.IMarkerService {
	m := &GMarker{}
	return m
}

func (mk *GMarker) InitServerSeed(serverSeed, nextServerSeed string) {
	mk.serverSeed = serverSeed
	mk.nextSeed = nextServerSeed
}

func (mk *GMarker) NextServerSeed256() string {
	return secure.Sha256Hex(byteutil.StringToSliceByte(mk.nextSeed))
}

func (mk *GMarker) RefreshSeed(nextServerSeed string) {
	mk.serverSeed = mk.nextSeed
	mk.nextSeed = nextServerSeed
}

func (mk *GMarker) ServerSeed() string {
	return mk.serverSeed
}

func (mk *GMarker) SetBet(betQ *statement.PlayBet, betR *statement.MinesBetInfo) {
	mk.mu.Lock()
	mk.betQ = betQ
	mk.betR = betR
	mk.mu.Unlock()
}

func (mk *GMarker) GetBetQ() *statement.PlayBet {
	return mk.betQ
}

func (mk *GMarker) GetBetR() *statement.MinesBetInfo {
	return mk.betR
}

func (mk *GMarker) CheckInOpen() bool {
	mk.mu.Lock()
	defer mk.mu.Unlock()
	if !mk.inOpen {
		mk.inOpen = true
		return false
	}
	return true
}

func (mk *GMarker) IsOpening() bool {
	return mk.inOpen
}

func (mk *GMarker) ClearOpen() {
	mk.mu.Lock()
	mk.inOpen = false
	mk.mu.Unlock()
}
